#include<iostream>
#include<cstring>
using namespace std;
char DuiYing[]={'1','0','X','9','8','7','6','5','4','3','2','1'};
int YanZheng[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
class CDate
{
    private:
	    int year, month, day;
    public:
	    CDate(int _ye, int _mo, int _da):year(_ye),month(_mo),day(_da){ }
	    bool check()
        {
            if(year>2021)
                return false;
            if(year==2021 && month>11)
            {
                return false;
            }
            if(year==2021 && month==11 && day>8)
            {
                return false;
            }
            if(month<1 || month>12)
                return false;
            if(month==1 || month==3 ||month==5||month==7||month==8||month==10||month==12)
            {
                if(day<1 || day>31)
                    return false;
            }
            else if(month !=2)
            {
                if(day<1 || day>30)
                    return false;
            }
            else if(isLeap())
            {
                if(day<1 ||day>29)
                    return false;
            }
            else
            {
                if(day<1 || day>28)
                    return false;
            }
            return true;
        }
	    bool isLeap()
        {
            return (year%4==0 && year%100!=0)||(year%400==0);
        }
	    void print()
        {
            cout<<year<<"年"<<month<<"月"<<day<<"日 ";
        }
        int getYear()
        {
            return year;
        }
        int getMonth()
        {
            return month;
        }
        int getDay()
        {
            return day;
        }
};
class COldId
{
    protected:
	    char* pId15, * pName; //15位身份证号码，姓名
	    CDate birthday; //出生日期
    public:
	    COldId(char* pIdVal, char* pNameVal, CDate& day):birthday(day)
        {
            pId15=new char[strlen(pIdVal)+1];
            strcpy(pId15,pIdVal);
            pName=new char[strlen(pNameVal)+1];
            strcpy(pName,pNameVal);
        }
	    bool check()
        {
            if(strlen(pId15)!=15)
                return false;
            for(int i=0;i<15;++i)
            {
                if(pId15[i]<'0' || pId15[i]>'9')
                    return false;
            }//???判断标准是啥...
            return true;
        }
	    void print();//打印啥?
	    ~COldId()
        {
            delete[] pId15;
            delete[] pName;
        }
};
class CNweId:public COldId
{
    protected:
        char *pId18;
        CDate issueDay;//签发日期
        int validYear;
    public:
        CNweId(char* pId15Val, char* pNameVal, CDate& day,char* pId18Val,CDate& issD,int _vYear):COldId(pId15Val,pNameVal,day),issueDay(issD)
        {
            pId18=new char[strlen(pId18Val)+1];
            strcpy(pId18,pId18Val);
            validYear=_vYear;
        }
        bool check()
        {
            //基本情况的排除
            if(strlen(pId18)!=18 || !issueDay.check() || !birthday.check())
                return false;
            //只有最后第17位能够为X
            for(int i=0;i<=16;++i)
            {
                if(pId18[i]<'0' || pId18[i]>'9')
                    return false;
            }

            //检验码排除
            int total=0;
            for(int i=0;i<=16;++i)
            {
                int tmp=pId18[i]-'0';
                total=total+tmp*YanZheng[i];
            }
            total=total%11;
            if(pId18[17]!=DuiYing[total])
                return false;
            
            //出生日期是否合法
            long long Bir=birthday.getYear()*10000+birthday.getMonth()*100+birthday.getDay();
            long long tmpBir=0;
            for(int i=6;i<=13;++i)
            {
                tmpBir*=10;
                int tmp=pId18[i]-'0';
                tmpBir+=tmp;
            }
            if(tmpBir!=Bir)
                return false;
            
            //是否还在有效期
            if(validYear<100)//非长期
            {
                //有效期只有年
                long long Allday=(issueDay.getYear()+validYear)*10000+issueDay.getMonth()*100+issueDay.getDay();
                long long Today=2021*10000+11*100+8;
                if(Allday<Today)//有效期比今天小
                    return false;
            }
            
            //是否由15位扩展而来
            int k=0;
            for(int i=0;i<15;++i)
            {
                if(pId15[i]!=pId18[k])
                    return false;
                k++;
                if(k==6)
                    k=8;
            }
            return true;
        }
        void PrintName()
        {
            cout<<pName<<endl;
        }
        void print()
        {
            cout<<pId18<<" ";
            issueDay.print();
            if(validYear>=100)
            {
                cout<<"长期"<<endl;
            }
            else
            {
                cout<<validYear<<"年"<<endl;
            }
        }
        ~CNweId()
        {
            delete[] pId18;
        }
};
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        char name[10];
        int BirY,BirM,BirD;
        char OldID[16];
        char NewID[19];
        int Iy,Im,Id;
        int validYear;
        cin>>name>>BirY>>BirM>>BirD>>OldID>>NewID>>Iy>>Im>>Id>>validYear;
        CDate birthday(BirY,BirM,BirD);
        CDate Issue(Iy,Im,Id);
        CNweId ans(OldID,name,birthday,NewID,Issue,validYear);
        ans.PrintName();
        if(ans.check())
        {
            ans.print();
        }
        else
        {
            cout<<"illegal id"<<endl;
        }
    }
}